package com.jhd.fuzhou.view.receipt;

import androidx.annotation.NonNull;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.github.chrisbanes.photoview.PhotoView;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.view.login.BaseActivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;

/*收文管理文书点击单个详情*/
public class PDFActivity extends BaseActivity {
    @BindView(R.id.tv_document_name)TextView tv_document_name;
    @BindView(R.id.pdfView) PDFView pdfView;
    @BindView(R.id.image)PhotoView image;
    private Context context=this;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_pdf);
    }

    @Override
    protected void processLogic() {
        onBack();
        tv_document_name.setText(getIntent().getStringExtra("name"));
        if("pdf".equals(getIntent().getStringExtra("type"))){
            image.setVisibility(View.GONE);
            pdfView.setVisibility(View.VISIBLE);

            new Thread(){
                @Override
                public void run() {
                    super.run();
                    getUrlPdf(getIntent().getStringExtra("url"));
                }
            }.start();
        }else{
            image.setVisibility(View.VISIBLE);
            pdfView.setVisibility(View.GONE);
            Glide.with(PDFActivity.this)
                    .load(getIntent().getStringExtra("url"))
                    .into(image);
        }

    }

    /*根据url下载pdf*/
    public void getUrlPdf(String url) {
        try {
            URL picurl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) picurl
                    .openConnection();
            conn.setConnectTimeout(6000);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.connect();
            InputStream is = conn.getInputStream();String fileName = "Download/show.pdf";//文件名
            File dir = new File(getExternalFilesDir(null).getPath() + "/Download/");//路径
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(getExternalFilesDir(null).getPath() + "/" + fileName);
            try {
                int index;
                byte[] bytes = new byte[1024];
                FileOutputStream outputStream = new FileOutputStream(file);
                while ((index = is.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                }
                outputStream.flush();
                outputStream.close();
                is.close();
                handler.sendEmptyMessage(1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    File file = new File(getExternalFilesDir(null).getPath() + "/Download/show.pdf" );
                    showPDF(pdfView,file,context);
                    break;
            }

        }
    };

    public static void showPDF(PDFView pdfView, File file, Context context){
        try {
            pdfView.fromStream(new FileInputStream(file))
                    .enableSwipe(true)
                    .swipeHorizontal(false)
                    .enableDoubletap(true)
                    .defaultPage(0)
                    .enableAnnotationRendering(false)
                    .password(null)
                    .scrollHandle(new DefaultScrollHandle(context))
                    .enableAntialiasing(true)
                    .spacing(0)
                    .autoSpacing(false)
                    .pageFitPolicy(FitPolicy.WIDTH)
                    .fitEachPage(false)
                    .pageSnap(false)
                    .pageFling(false)
                    .nightMode(false)
                    .load();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}